import { getToken } from '@/utils/auth'
import Http from '@/utils/httpUtil'

const user = {
  state: {
    token: getToken(),
    userInfo: {},
    userid: '',
    usertype: '',
    userType: '',
    type: '',
    username: '',
    orgId: '',
    role: ''
  },

  mutations: {
    SET_USER_INFO: (state, info) => {
      state.userInfo = {
        ...state.userInfo,
        ...info
      }
      state.userid = info.phid
      state.role = info.role
      state.usertype = info.userType
      state.userType = info.userType
      state.type = info.userType
      state.username = info.userName
      state.orgId = info.orgId
      state.orgId = info.orgId
    }
  },

  actions: {
    // 用户名登录
    LoginByUsername({ commit }, userInfo) {
      const param = {
        usersName: userInfo.username.trim(),
        passWord: userInfo.password.trim()
      }
      return Http.post('ucenter/users-local-oauth/login', {
        ...param
      }).then((data) => {
        window._token = data.data.token
        sessionStorage.setItem('token', data.data)
        // return data.data
      })
    },
    getUserInfo({ commit }, noAuth) {
      return Http.get('ucenter/users-info/getUserInfoByToken', {}, { noAuth: !!noAuth }).then((res) => {
        commit('SET_USER_INFO', res.data)
        return res.data
      }).catch((err) => {
        console.log('err', err)
      })
    },
    CheckLogin({ commit }, userInfo) {
    }
  }
}

export default user
